#!/bin/zsh
## Vi/Emacs-style keybindings
# bindkey -v
# setopt VI
bindkey -e
setopt EMACS

# use `cat` to get the proper sequences for certain key combinations
bindkey "^[[1;5D" beginning-of-line # CTRL + <
bindkey "^[OH"    beginning-of-line # HOME
bindkey "^[[1;5C" end-of-line       # CTRL + >
bindkey "^[OF"    end-of-line       # END
bindkey "^[[1;3D" backward-word     # ALT  + <
bindkey "^[[1;3C" forward-word      # ALT  + >
bindkey "^[[3~"   delete-char       # DELETE
bindkey "^[[A"    history-search-backward # up   arrow
bindkey "^[[B"    history-search-forward  # down arrow
bindkey "^[[2~"   overwrite-mode          # Insert

bindkey '^[h'     backward-word        # Alt + h
bindkey '^[l'     forward-word         # Alt + l
bindkey '^[j'     backward-char        # Alt + j
bindkey '^[k'     forward-char         # Alt + k

bindkey '^[y'     backward-delete-word # Alt + y
bindkey '^[o'     delete-word  # Alt + o
bindkey '^[u'     backward-delete-char # Alt + u
bindkey '^[i'     delete-char  # Alt + i

bindkey '^[^H'    backward-delete-word # Ctrl + h
bindkey '^[^L'    delete-word          # Ctrl + l
bindkey '^[y'     redo                 # Alt + y
bindkey '^[z'     undo                 # Alt + z
bindkey '^L'      clear-screen         # Ctrl + l


## insert sudo {{{
sudo-command-line() {
    [[ -z $BUFFER ]] && zle up-history
    [[ $BUFFER != sudo\ * ]] && BUFFER="sudo $BUFFER"
    zle end-of-line                 # move cursor to end-of-line
}
zle -N sudo-command-line
bindkey "\e\e" sudo-command-line    # [Esc] [Esc]
#}}}


##空行(光标在行首)补全 "cd " {{{
user-complete(){
    case $BUFFER in
        "" )                       # 空行填入 "cd "
        BUFFER="cd "
        zle end-of-line
        zle expand-or-complete
        ;;
        * )
        zle expand-or-complete
        ;;
    esac
}
zle -N user-complete
bindkey "\t" user-complete
#}}}
